/* 此文件用于生成可选择导出的属性的数据结构*/

const objectMerge = function (propertyData, allProperty, type) {
	for (const i in allProperty) {
		const obj = {
			value: i
		}
		if (i === 'baseInfo') {
			obj.label = '基础信息'
		} else if (i === 'consignerInfo') {
			obj.label = '委托人信息'
		} else if (i === 'dateInfo') {
			obj.label = '重要日期信息'
		} else if (i === 'investInfo') {
			obj.label = '投资相关信息'
		} else if (i === 'organInfo') {
			obj.label = '机构信息'
		} else if (i === 'riskInfo') {
			obj.label = '风控信息'
		} else if (i === 'rateInfo') {
			obj.label = '费率信息'
		} else if (i === 'fundManagerInfoList') {
			obj.label = '基金经理'
		}
		propertyData.push(obj)
	}

	for (let index = 0; index < propertyData.length; index++) {
		const property = propertyData[index].value
		if (Object.prototype.toString.call(allProperty[property]).substring(8, Object.prototype.toString.call(allProperty[property]).length - 1) === 'Object') {
			const list = Object.keys(allProperty[property])
			propertyData[index].children = []
			for (let i = 0; i < list.length; i++) {
				const data = allProperty[property]
				const obj = {
					value: list[i],
					label: data[list[i]]
				}
				if (list[i] === 'fundCode' || list[i] === 'fundFullName') {
					obj.disabled = true
				}
				propertyData[index].children.push(obj)
			}
		}
	}
}

export const fundTypePublic = function (type) {
	const allProperty = {
		baseInfo: {},
		consignerInfo: {
			consignerName: '委托人名称',
			subjectType: '主体类型',
			csrcTypeName: '证监会分类',
			certificateTypeName: '证件类型',
			certificateNumber: '证件号码',
			peopleMaxName: '人行大类',
			peopleMinName: '人行小类',
			province: '省或直辖市',
			city: '市或区'
		},

		dateInfo: {
			raiseStartDate: '募集起始日',
			nextOpenFirstDate: '下次开放期首日',
			formalEvolveDate: '正式转型日期',
			contractEndDate: '合同预计到期日（证监）',
			contractEndDatePeople: '合同预计到期日（人行）',
			liquidateAccompleteDate: '清盘完成日',
			endListDate: '终止上市日', // 公募
			approvalDate: '核准日期', // 公募
			logonApprovalChangeDate: '注册批文变更日期', // 公募
			deferRaiseDate: '延期募集备案日期', // 公募
			firstRedemptionDate: '首次打开赎回日', // 公募
			initialFilingDate: '初始备案日期', // 专户
			finishFilingDate: '完成备案日期', // 专户
			establishDate: '产品成立日期',
			operateEndDate: '合同实际到期日',
			liquidationStartDate: '清算开始日期'
		},

		investInfo: {
			isQuantTactics: '是否采用量化策略', // 公募
			isCoverTactics: '是否采用对冲策略', // 公募
			stockMinRatio: '股票类资产投资最低比例',
			stockMaxRatio: '股票类资产投资最高比例',
			hkStockMaxRatio: '香港市场股票类资产投资最高比例',
			bondMinRatio: '债券类资产投资最低比例',
			bondMaxRatio: '债券类资产投资最高比例',
			currencyMinRatio: '货币类资产投资最低比例',
			currencyMaxRatio: '货币类资产投资最高比例',
			isContainSpclLimit: '是否包含特殊投资限制',
			spclLimitRemark: '特殊投资限制备注',
			specialTypeChar: '是否特殊品种',
			remark: '备注'
		},

		rateInfo: {
			isAchieveReward: '是否提取业绩报酬',
			accrualPointTime: '计提时点',
			accrualDatum: '计提基准',
			accrualRatio: '计提比例',
			rewardExplain: '业绩报酬说明',
			feeRateInfoList: '管理、托管、销售服务费率及备注'
		},

		riskInfo: {
			isRiskMeasures: '是否有风控措施：预警、止损、最大回撤及其他风控措施等',
			investLeverRate: '投资杠杆率上限（总资产/净资产）%',
			earlyWarnLine: '有无预警线',
			earlyWarnLineType: '预警线类型',
			earlyWarnNetWorth: '预警线对应的资产管理计划净值',
			stopLossLine: '有无止损线',
			stopLossLineType: '止损线类型',
			stopLossNetWorth: '止损线对应的资产管理计划净值',
			maxRetreat: '有无回撤预警线',
			retreatWarnLineType: '回撤预警线类型',
			retreatEarlyWarnNetWorth: '回撤预警线对应的资产管理计划净值',
			stopLossWarningLine: '有无回撤止损线',
			retreatStopLossLineType: '回撤止损线类型',
			retreatStopLossNetWorth: '回撤止损线对应的资产管理计划净值',
			otherRiskStep: '有无其他风控措施',
			otherRiskExpression: '其他风控措施-合同表述',
			managerIsInvestment: '管理人是否投入资金',
			managerInvestmentAmount: '管理人投入资金参与金额',
			managerInvestmentRatio: '管理人投入资金参与比例',
			managerIsPartake: '管理人附属机构自有资金是否参与',
			managerPartakeAmount: '管理人及其附属机构自有资金参与金额（万元）',
			managerPartakeRatio: '管理人及其附属机构自有资金参与比例（%）'
		},

		organInfo: {
			bordersCustodianName: '境外托管人中文名称',
			bordersCustodianEnglishName: '境外托管人英文名称',
			bordersAdvisorName: '境外投资顾问中文名称',
			bordersAdvisorEnglishName: '境外投资顾问英文名称',
			isThirdPartyCustody: '是否第三方托管',
			custodyBranchOrganName: '托管人分支机构名称',
			fundTaType: '登记机构代码',
			fundTaTypeName: '登记机构名称',
			handleLawerName: '经办律师名称',
			managementRegisterNumber: '管理人代码',
			managementFullName: '管理人名称',
			trusteeRegisterNumber: '托管人代码',
			trusteeFullName: '托管人名称',
			lawFirmRegisterNumber: '律师事务所代码',
			lawFirmName: '律师事务所名称',
			accountFirmRegisterNumber: '会计师事务所代码',
			accountFirmName: '会计师事务所名称',
			advisorRegisterNumber: '投资顾问代码',
			investAdvisorName: '投资顾问中文名称',
			advisorEnglishName: '投资顾问英文名称',
			investAdvisorOrgan: '投资顾问机构类型', // 专户
			investAdvisorRate: '投资顾问费率' // 专户
		},

		fundManagerInfoList: '基金经理'
	}

	// 公募/专户共有字段
	const publicPropery = {
		fundCode: '产品代码（六位）',
		evolveBeforeCode: '转型前产品正式代码',
		performanceBaseline: '业绩比较基准',
		fundName: '产品简称',
		fundFullName: '产品名称',
		fundEnglishName: '产品英文名称',
		isInnovateNature: '是否具备创新属性',
		innovateNatureRemark: '创新属性',
		fundType: '产品类别',
		isActiveManage: '是否为主动管理',
		operateMode: '运作方式',

		fiduciaryDuty: '受托责任',
		expectRaiseAmount: '预期募集金额',
		settleCurrency: '结算币种',
		faceValue: '面值（单位:元）',
		isNetWorthProduct: '是否净值型产品',
		valuationWay: '估值方法',
		valuationFrequency: '估值频率',
		isSubAssets: '是否子资产单元',
		momFundCode: '管理人中管理人产品代码',
		isAddcreditMeasure: '产品资产是否存在增信措施（人行二期）',
		oneYardPassAccount: '一码通账户账号',
		remark: '备注',
		isQdii: '是否QDII',
		isMom: '是否MOM',
		isIndexFund: '是否指数基金',
		riskGrade: '风险等级',
		productBrand: '产品品牌（人行二期）',
		productPeriod: '产品期次（人行二期）（四位）',
		managerStyle: '管理方式（人行二期）',
		issuerEarlyTermFlag: '是否有发行机构提前终止权（人行二期）',
		usufructTransferProductFlag: '是否收益权转让产品（人行二期）',
		currencyMarketFundFlag: '是否货币市场基金（人行二期）',
		isFofFund: '是否FOF基金',
		establishScale: '成立规模',
		isListTransfer: '是否挂牌转让',
		productAssetReportType: '产品类型（证监会资管月报）',
		addcreditMeasureType: '增信措施类别',
		addCreditOrganType: '增信机构类型（人行二期）',
		addCreditFormType: '增信形式（人行二期）',
		customerRedemptionFlag: '客户赎回权标识',
		noneHKTIsStopApplyRansom: '非港股通交易日是否暂停申赎',
		crossBorderFinancial: '跨境理财通（人行）',
		propertyFundTrust: '以财产权信托名义开展的资金信托（人行）'
	}

	// 公募基础信息字段
	const publicBaseInfo = {
		newspaperId: '报社',
		isBreakeven: '是否保本',
		isHoldFund: '是否持有期基金',
		isOrganCustom: '是否机构定制基金',
		organCustom: '定制机构信息',
		isSingleShare: '是否单一份额',
		approvalSymbol: '核准文号',
		isGradFund: '分级基金类型',
		isPensionTargetFund: '是否养老目标基金',
		investTactics: '投资策略',
		isMutualFund: '是否内地互认基金',
		isRiskTacticsFund: '是否避险策略基金',
		ensureObligorCode: '保障义务人代码',
		ensureObligorName: '保障义务人名称',
		mixedInvestBias: '混合型细分类',
		isMarketTrade: '是否上市交易',
		marketFundType: '上市基金类型',
		marketTradeSite: '上市交易场所',
		isEtfJoinFund: '是否ETF联接基金',
		targetFundCode: '目标基金代码',
		targetFundName: '目标基金名称',
		revenueFeature: '收益特征',
		pobcFundType: '产品类型（人行二期）',
		isSponsoredFund: '是否发起式基金',
		sponsoredSelfCapital: '发起式自有资金',
		sponsoredAgreedTerm: '发起式约定年限',
		branchShareList: '下属列表',
		openFrequency: '开放频率（证监）',
		openFrequencyRemark: '开放频率（证监）备注',
		...publicPropery
	}

	// 专户基础信息字段
	const speciaBaseInfo = {
		associationOpenFrequency: '开放频率（协会）',
		fundOutsideCode: '协会代码',
		productAssetType: '产品类型-资产投向',
		productSpecialType: '产品类型-专项',
		planType: '计划类型',
		isStructuredProduct: '是否结构化产品',
		isPrivateFund: '是否私募基金',
		privateFundType: '私募基金类别',
		incomeDisWay: '收益分配方式',
		isHolderMeet: '是否设立持有人大会',
		isChannelBusiness: '是否通道类业务',
		gradingRatio: '分级比例',
		isSamp: '是否专项资产管理计划',
		isPensionProduct: '是否养老金产品',
		isInstAcc: '是否分期账户',
		isBasicPension: '是否基本养老金',
		isSocialSecurity: '是否社保基金',
		isAnnuityPension: '是否企业年金',
		annuityPlanType: '企业年金计划类型',
		annuityTrusteeMode: '企业年金受托模式',
		isOccupationalPension: '是否职业年金',
		isSpecialAssetReport: '是否专项（证监会资管月报）',
		isAssetSecuritizationBusiness: '是否资产证券化业务（证监会资管月报）',
		isAssetManagementProduct: '是否为资管产品投资单一项目或证券（证监会资管月报）',
		openFrequency: '开放频率',
		openFrequencyRemark: '开放频率备注',
		...publicPropery
	}

	const propertyData = []

	if (type === '1') {
		allProperty.baseInfo = publicBaseInfo
	} else {
		allProperty.baseInfo = speciaBaseInfo
	}
	objectMerge(propertyData, allProperty, type)
	propertyData.push({ value: 'status', label: '运作状态', disabled: true })
	propertyData.push({ value: 'personLiableId', label: '责任人' })
	propertyData.push({ value: 'checkLiableId', label: '复核人' })
	return propertyData
}

export function listItem(type) {
	const list = fundTypePublic(type)
	const itemArray = []
	list.forEach(item => {
		if (item.children) {
			itemArray.push(...item.children)
		} else {
			itemArray.push(item)
		}
	})
	return itemArray
}

// 将所选的属性转成GragphQL需要的类型数据
export function propertyString(list) {
	let msg = ''
	const obj = {}
	if (list.length) {
		list.forEach(item => {
			if (item.length === 2) {
				const property = item[1]
				obj[item[0]] = { [property]: '', ...obj[item[0]] }
			} else {
				obj[item[0]] = ''
			}
		})
		if (obj.riskInfo) {
			const riskInfoList = Object.keys(obj.riskInfo).reverse()
			obj.riskInfo = {}
			riskInfoList.forEach(item => {
				obj.riskInfo[item] = ''
			})
		}
		for (const i in obj) {
			if (Object.prototype.toString.call(obj[i]).substring(8, Object.prototype.toString.call(obj[i]).length - 1) === 'Object') {
				msg += `${i}{${objectKeys(Object.keys(obj[i]))}},`
			} else if (i === 'fundManagerInfoList') {
				const fundMangeProperty = [
					'fundManagerId',
					'managerId',
					'workflowStartDate',
					'workflowStatus',
					'workflowId',
					'startDate',
					'endDate',
					'entryNoticeDate',
					'leaveNoticeDate',
					'fundChangeHistory',
					'remarks',
					'fundType',
					'manageStatus',
					'entryReportBodDate',
					'entryBodResolutionDate',
					'entrySubmittedDate',
					'entrySocietyReplyDate',
					'entryScanPiece',
					'leaveReportBodDate',
					'leaveBodResolutionDate',
					'leaveSubmittedDate',
					'leaveSocietyReplyDate',
					'leaveScanPiece',
					'generalMeetingReplyDate',
					'managerName',
					'oaId',
					'email',
					'phone',
					'mobile',
					'gender',
					'birthYear',
					'idType',
					'idNo',
					'entryYear',
					'servingDate',
					'leaveDate',
					'education',
					'resumeDocId',
					'resume',
					'remark',
					'departmentId',
					'job',
					'entryFundManagerDate',
					'entryAccountManagerDate',
					'entryResearcherDate',
					'managerType',
					'fundManagerType',
					'idEndDate',
					'stockEntryStartDate',
					'yearsOfEmployment'
				]
				msg += `fundManagerInfoList{${fundMangeProperty.join(',')}},`
			} else {
				msg += `${i},`
			}
		}
		const endMsg = msg.substring(msg.length - 1, msg.length)
		if (endMsg === ',') {
			msg = msg.substring(0, msg.length - 1)
		}
	}

	return msg
}

// 将属性集转换成字符串
function objectKeys(propertyArray) {
	let msg = ``
	propertyArray.forEach(item => {
		if (item === 'feeRateInfoList') {
			msg += `${item}{feeRateId,feeRateType,startSection,endSection,feeRate,feeRateUnit,belongFundRatio,belongFundRatioUnit,remark,fundCode,subFundType,salesServiceFeeAlgorithm},feeRateAlgorithm,custodyFeeAlgorithm,`
		} else if (item === 'branchShareList') {
			msg += `${item}{fundCode,fundGradName,fundGradFullName,subFundType,shareCategory,estimatedRaisedAmount,estimatedAnnualRate},`
		} else {
			msg += `${item},`
		}
	})
	const endMsg = msg.substring(msg.length - 1, msg.length)
	if (endMsg === ',') {
		msg = msg.substring(0, msg.length - 1)
	}
	return msg
}

